package ua.flatmap.processor.realt;

import com.flatsmap.dao.StreetDao;
import com.flatsmap.dto.PoiDTO;
import com.flatsmap.service.PoiBaseService;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopScoreDocCollector;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;
import org.springframework.util.StringUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import ua.flatmap.processor.XMLReader;
import ua.flatmap.util.AddressToCoordinatesRetriever;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.IOException;
import java.io.StringReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: Kostya
 * Date: 14.07.11
 */
public class RealtXMLReader extends XMLReader {

    public void execute(String result) {
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            InputSource is = new InputSource(new StringReader(result));
            Document doc = db.parse(is);
            doc.getDocumentElement().normalize();
            NodeList appartments = doc.getElementsByTagName("appartment");

            for (int s = 0; s < appartments.getLength(); s++) {

                Node fstNode = appartments.item(s);

                if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
                    PoiDTO newPOI = new PoiDTO();
                    Element fstElmnt = (Element) fstNode;

                    String priceStr = getNodeValue(fstElmnt, "price");
                    priceStr = StringUtils.replace(priceStr, "грн.", "");
                    priceStr = StringUtils.replace(priceStr, " ", "");
                    Double price = Double.parseDouble(priceStr);
                    newPOI.setPrice(price);

                    String roomNumberString = getNodeValue(fstElmnt, "room");

                    newPOI.setRoomNumber(0);
                    for (Integer i = 1; i < 7; i++) {
                        if (roomNumberString.contains(i.toString())) {
                            newPOI.setRoomNumber(i);
                            break;
                        }
                    }


                    String descValue = getNodeValue(fstElmnt, "description");
                    descValue.replace("Если Вы не видите на карте соответствующий адрес -\n" +
                            "значит он указан неверно или его нет в базе Google ", "");
                    newPOI.setDescription(descValue);
                    String street;

                    street = getNodeValue(fstElmnt, "address");
                    street = street.substring(street.indexOf("Киев") + 5);

                    newPOI.setStreet(street);
                    Double[] coordinates = AddressToCoordinatesRetriever.getCoordinates(street);
                    newPOI.setLng(coordinates[0]);
                    newPOI.setLat(coordinates[1]);
                    poiService.save(newPOI);
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }




}
